$(document).ready(function(){

show_text_area_status=function(ops)
{
var idn=$('#'+ops.idn);
var classShow=$('.'+ops.classShow);
var textarea_auto=document.getElementById(ops.idn);
textarea_auto.style.backgroundColor='#ffffff';textarea_auto.style.color='#000';textarea_auto.style.height='auto';
if(idn.attr('rows')<3){
idn.attr('rows',3);}
classShow.show();
$('div.tit_post_ch').show();
//$('.title_post_ch').focus();
}

add_comment_text=function(option)
{
	var id=$('#'+option.id);
	var newid=$('#new'+option.id);
	id.focusout(function(){
	id.unbind('keypress');
	return false;
	});
		
	id.keypress(function(event){
	if(event.which==13)
	{	
		$.ajax({
		url:"comments/index/add",
		// url:"http://localhost/hoidap/Source/comments/index/add",
		data:{content: $(id).val(), author: option.author, parent: option.id },
		type:'post',
		dataType:'html',
		success:function(html)
		{	
			$(newid).append(html);
			id.val('');
			return false;			 
		}
		});
	
	}
	});
	return false;	
}

catelogy_select_text=function(option)
{
	var id=$('#'+option.id);

	if(option.parent == 0)
	{	
		$(id).html("<select id='categories_con'  name='categories_con'><option value='0'>Chọn Danh mục câu hỏi</option></select>");
		return false;
	}
	else if(option.parent == -1)
	{	
	//	$(id).html("<select  name='categories_con'><option value='0'>Chọn Danh mục câu hỏi</option></select>");
		catelogy_select_listall();
		return false;
	}		
	else
	{	
			$.ajax({
			url:"categories",
			// url:"http://localhost/hoidap/Source/categories",
			data:{name:'con', parent: option.parent, limit: 0},
			type:'post',
			dataType:'html',
			success:function(html)
			{
				$(id).html(html);
				return false;			 
			}
			});
		return false;	
	}
}


catelogy_select_listall=function()
{		
		var id=$('#categories_cha');
		$.ajax({
		url:"categories",
		// url:"http://localhost/hoidap/Source/categories",
		data:{name:'cha', parent: 0, limit: 0},
		type:'post',
		dataType:'html',
		success:function(html)
		{	
			$(id).html(html);
			return false;			 
		}
		});
		return false;	
}

//Dang cau hoi
waiting_load_status_and_question=function(ops){
	var noidung=$('.'+ops.noidung);
	var catcha=$('#'+ops.catcha);
	var catcon=$('#'+ops.catcon);
	var new_post =$('#new_post');
	var tieude=$('.'+ops.tieude);	
	
	catcha.css( 'border-color','#FFFFFF');
	catcon.css( 'border-color','#FFFFFF');
	
	var meta_tile=$('.mg_top_5 span').html();
	var meta_image=$('.img_link_post img').attr("src");
	var meta_link=$('#meta_link').html();
	var meta_content=$('#meta_content').html();

	var post_image = $('#img_post_ch').val();
	// alert(post_image);
	
	if(noidung.val()=="")
	{
		alert('Nhập nội dung câu hỏi');
		noidung.focus();
		return false;
	}	
	else if(tieude.val()=="")
	{
		alert('Nhập tiêu đề câu hỏi');
		tieude.focus();
		return false;
	}	
	
	
	else if(catcha.val()=="0")
	{
		
		catcha.focus();			
		alert('Vui lòng loại câu hỏi');
		catcha.css( 'border-color','#F90');
		return false;
	}
	
	else if(catcon.val()=="0")
	{
		
		catcon.focus();			
		alert('Vui lòng chọn mục câu hỏi');
		catcon.css( 'border-color','#F90');
		return false;
	}

	else
	{
		$.ajax({
		url:"Posts/index/addpostuser",
		// url:"http://localhost/hoidap/Source/Posts/index/addpostuser",
		data:{content:noidung.val(), catcha: catcha.val(), catcon: catcon.val(),
		meta_tile:meta_tile, meta_image:meta_image, meta_link:meta_link, meta_content:meta_content,
		post_title:tieude.val(), post_image:post_image, post_type:'1'
		},
		type:'post',
		dataType:'html',
		success:function(html)
		{	
			noidung.val('');
			new_post.html(html);
			new_post.attr('id', '');
			$('#img_post_ch').val('');
			$('.title_post_ch').val('');
			$('.image_list_ch').html('');
			$('.panel_posted_status_edit_img').hide();	
			$('.input').val('');	
			return false;			 
		}
		});

	}

	return false;	
		
}


$('#formdangky').hide();

like_popup=function(idpost)
{
 // alert(idpost.type);
	$('#formdangky').load('likes/index/getuserlikesbypost?post='+idpost.id+'&table='+idpost.type);	
	// $('#formdangky').load('http://localhost/hoidap/Source/likes/index/getuserlikesbypost?post='+idpost.id+'&table='+idpost.type);
	$('#formdangky').fadeIn('show');
}


$('#btn_close_dk').live('click',function(){
		$('#formdangky').load('likes/index/getuserlikesbypost?post=0&table=0');	
		// $('#formdangky').load('http://localhost/hoidap/Source/likes/index/getuserlikesbypost?post=0&table=0');	
		$('#formdangky').fadeOut();
		
	});

	

$('#comfirmdelete').hide();

comfirmdelete_popup=function(ops)
{
 // alert(ops.type);
  
  if(ops.type =="post")
  {
	$('#comfirmdelete').load('Posts/index/deletecomfirm?post_id='+ops.id);		
  }
  else if(ops.type =="comments")
  {
	$('#comfirmdelete').load('comments/index/deletecomfirm?comments_id='+ops.id);		
  }
	$('#comfirmdelete').fadeIn('show');
}


//tat popup
comfirm_off=function()
{
	$('#comfirmdelete').fadeOut();
}

/* end dang lam xac nhan xoa */
	
// Dang album hinh anh
submit_images_album=function(ops){
	var noidung=$('.'+ops.noidung);	
	var new_post =$('#new_post');
	var post_image = $('#img_post_ha').val();

		$.ajax({
		url:"Posts/index/addpostuser",
		// url:"http://localhost/hoidap/Source/Posts/index/addpostuser",
		data:{content:noidung.val(), catcha:'', catcon:'',
		meta_tile:'', meta_image:'', meta_link:'', meta_content:'',
		post_title:'', post_image:post_image,post_type:'2'
		},

		type:'post',
		dataType:'html',
		success:function(html)
		{
			noidung.val('');
			new_post.html(html);
			new_post.attr('id', '');
			$('#img_post_ha').val('');		
			$('.image_list').html('');
			return false;			 
		}
		});
		
}


	
// xoa post
delete_post=function(ops){

$('.generic_dialog_popup').fadeOut();
$('#listPost_'+ops.id).fadeOut();
// alert(ops.id);

		$.ajax({
		url:"Posts/index/delete",
		data:{id_post:ops.id},
		type:'post',
		dataType:'html',
		success:function()
		{
			//alert('2222');
			return false;			 
		}
		});
		
		$.ajax({
		url:"comments/index/deletebypost",
		data:{id_post:ops.id},
		type:'post',
		dataType:'html',
		success:function()
		{
			//alert('comments');
			return false;			 
		}
		});
		return false;		
}

// xoa comments
delete_comments=function(ops){

$('.generic_dialog_popup').fadeOut();
$('#replyList_'+ops.id).fadeOut();
 // alert(ops.id);
		
		$.ajax({
		url:"comments/index/delete",
		data:{id_comments:ops.id},
		type:'post',
		dataType:'html',
		success:function()
		{
			//alert('comments');
			return false;			 
		}
		});
		return false;		
}



});